from collections.abc import Iterable

# 判断对象是否是指定类型
result = isinstance((3, 5), Iterable)
print("元组是否是可迭代对象:", result)

result = isinstance([3, 5], Iterable)
print("列表是否是可迭代对象:", result)

result = isinstance({"name": "张三"}, Iterable)
print("字典是否是可迭代对象:", result)

result = isinstance("hello", Iterable)
print("字符串是否是可迭代对象:", result)

result = isinstance({3, 5}, Iterable)
print("集合是否是可迭代对象:", result)

result = isinstance(range(5), Iterable)
print("range是否是可迭代对象:", result)

result = isinstance(5, Iterable)
print("整数是否是可迭代对象:", result)

result = isinstance(5, int)
print("整数是否是int类型对象:", result)

class Student(object):
    pass

stu = Student()
result = isinstance(stu, Iterable)

print("stu是否是可迭代对象:", result)

result = isinstance(stu, Student)

print("stu是否是Student类型的对象:", result)



class MyList(object):
    def __init__(self) -> None:
        self.my_list = list()
        self.idx = 0

    def append_item(self, item):
        self.my_list.append(item)

    def __iter__(self):
        return self
        
    def __next__(self):
        if self.idx < len(self.my_list):
            res = self.my_list[self.idx]
            self.idx += 1
            return res
        else:
            raise StopIteration


ml = MyList()
ml.append_item(1)
ml.append_item(2)
res = isinstance(ml, Iterable)
print(res)
for v in ml:
    print(v)



def fibonacci(num):
    a = 0
    b = 1
    # 记录生成fibonacci数字的下标
    current_index = 0
    print("--11---")
    while current_index < num:
        result = a
        a, b = b, a + b
        current_index += 1
        print("--22---")
        # 代码执行到yield会暂停，然后把结果返回出去，下次启动生成器会在暂停的位置继续往下执行
        yield result
        print("--33---")
        return "嘻嘻"

fib = fibonacci(5)
# value = next(fib)
# print(value)
# value = next(fib)
# print(value)

# value = next(fib)
# print(value)

# value = next(fib)
# print(value)

# value = next(fib)
# print(value)

try:
    value = next(fib)
    print(value)
    value = next(fib)
    print(value)
except StopIteration as e:
    # 获取return的返回值
    print(e.value)


# for value in fib:
#     print(value)


import time

def work1():
    while True:
        print("----work1---")
        yield
        time.sleep(0.5)

def work2():
    while True:
        print("----work2---")
        yield
        time.sleep(2)

def main():
    w1 = work1()
    w2 = work2()
    while True:
        next(w1)
        next(w2)

# if __name__ == "__main__":
#     main()

import time

def crawl_page(url):
    print('crawling {}'.format(url))
    sleep_time = int(url.split('_')[-1])
    time.sleep(sleep_time)
    print('OK {}'.format(url))


def main(urls):
    for url in urls:
        crawl_page(url)

# begin_time = time.time()
# main(['url_1', 'url_2', 'url_3', 'url_4'])
# end_time = time.time()
# run_time = end_time - begin_time
# print("程序耗时{}s".format(run_time))

import time
import asyncio

async def crawl_page(url):
    print('crawling {}'.format(url))
    sleep_time = int(url.split('_')[-1])
    await asyncio.sleep(sleep_time)
    print('OK {}'.format(url))

async def main(urls):
    tasks = [asyncio.create_task(crawl_page(url)) for url in urls]
    for task in tasks:
        await task
    # await asyncio.gather(*tasks)

# begin_time = time.time()
# asyncio.run(main(['url_1', 'url_2', 'url_3', 'url_4']))
# end_time = time.time()
# run_time = end_time - begin_time
# print("程序耗时{}s".format(run_time))


import time
import asyncio

async def worker_1():
    print('worker_1 start')
    await asyncio.sleep(1)
    print('worker_1 done')

async def worker_2():
    print('worker_2 start')
    await asyncio.sleep(2)
    print('worker_2 done')

async def main():
    task1 = asyncio.create_task(worker_1())
    task2 = asyncio.create_task(worker_2())
    print('before await')
    
    await task2
    print('awaited worker_2')
    await task1
    print('awaited worker_1')

begin_time = time.time()
asyncio.run(main())
end_time = time.time()
run_time = end_time - begin_time
print("程序耗时{}s".format(run_time))

